package controllers.security;

import java.util.Date;

import framework.base.BaseController;
import framework.base.PagerRS;
import framework.utils.SecurityUtil;
import models.security.IPTable;
import models.security.vo.IPTableQuery;
import play.data.binding.ParamNode;
import play.data.validation.Valid;

public class IPTables extends BaseController {

    public static void index(Integer page,IPTableQuery query){
        PagerRS<IPTable> rs = (PagerRS<IPTable>) IPTable.findByPager(getQuery(query, IPTableQuery.class), getPageNo(page),
                getPageSize());
        render(rs);
    }
    
    public static void add(){
        render();
    }
    
    public static void create(@Valid IPTable tb){
        if(validation.hasErrors()){
            saveUserLogAndNotice("白名单创建失败", false);
            render("@add");
        }else{
            String loginerId = SecurityUtil.getLoginUserId();
            tb.creatorId = loginerId;
            tb.operatorId = loginerId;
            tb.save();
            saveUserLogAndNotice("白名单创建成功", true);
            index(null,null);
        }
       
    }
    
    public static void edit(String id){
        IPTable tb = IPTable.findById(id);
        render(tb);
    }
    
    public static void update(String id){
        IPTable tb = IPTable.findById(id);
        tb.edit(ParamNode.convert(params.all()), "tb");
        validation.valid(tb);
        if(validation.hasErrors()){
            saveUserLogAndNotice("白名单更新失败", false);
            render("@edit",tb);
        }else{
            String loginerId = SecurityUtil.getLoginUserId();
            tb.updatedAt = new Date();
            tb.operatorId = loginerId;
            tb.save();
            saveUserLogAndNotice("白名单更新成功",  true);
            index(null,null);
        }
    }
    
    
    public static void destroy(String id){
        IPTable tb = IPTable.findById(id);
        if(null!=tb){
            String loginerId = SecurityUtil.getLoginUserId();
            tb.operatorId = loginerId;
            tb.delete();
            saveUserLogAndNotice("白名单删除成功",  true);
        }else{
            saveUserLogAndNotice("白名单删除失败", false);
            
        }
        index(null,null);
    }
    
    
    public static void show(String id){
        IPTable tb = IPTable.findById(id);
        render(tb);
    }
    
    
}
